﻿namespace Budgetsch.WP7.View
{
    using System;
    using System.Windows;

    using Budgetsch.WP7.Model.DataService;
    using Budgetsch.WP7.Utils;

    using GalaSoft.MvvmLight.Ioc;

    /// <summary>
    /// Description for LoginView.
    /// </summary>
    public partial class LoginView
    {
        public AppSettings AppSettings
        {
            get
            {
                return SimpleIoc.Default.GetInstance<AppSettings>();
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            NavigationService.RemoveBackEntry();
        }

        /// <summary>
        /// Initializes a new instance of the LoginView class.
        /// </summary>
        public LoginView()
        {
            InitializeComponent();

            if (AppSettings.IsRemember.HasValue && AppSettings.IsRemember.Value)
            {
                ApplicationBar.IsVisible = false;
                SingInPanel.Visibility = Visibility.Collapsed;
                loadingControl.Visibility = Visibility.Visible;
                loadingControl.IsVisible = true;
                UserNameInput.Text = AppSettings.UserName;
                PasswordInput.Password = AppSettings.PasswordSetting;
                var client = SimpleIoc.Default.GetInstance<IAuthenticationService>();
                client.Login(
                    UserNameInput.Text,
                    PasswordInput.Password,
                    (result, exception) =>
                    {
                        if (result)
                        {
                            loadingControl.Visibility = Visibility.Collapsed;
                            loadingControl.IsVisible = false;
                            NavigationService.Navigate(new Uri("/View/MainPage.xaml", UriKind.Relative));
                        }
                        else
                        {
                            SingInPanel.Visibility = Visibility.Visible;
                            loadingControl.Visibility = Visibility.Collapsed;
                            loadingControl.IsVisible = false;
                            ApplicationBar.IsVisible = true;
                            MessageBox.Show("Login failed! Please try again.", "Error", MessageBoxButton.OK);
                        }
                    });
            }
        }

        private void LoginAppButton_Click(object sender, EventArgs e)
        {
            var client = SimpleIoc.Default.GetInstance<IAuthenticationService>();
            loadingControl.Visibility = Visibility.Visible;
            loadingControl.IsVisible = true;
            client.Login(
                UserNameInput.Text,
                PasswordInput.Password,
                (result, exception) =>
                {
                    if (result)
                    {
                        AppSettings.UserName = UserNameInput.Text;
                        AppSettings.PasswordSetting = PasswordInput.Password;
                        AppSettings.IsRemember = RememberMe.IsChecked;
                        loadingControl.Visibility = Visibility.Collapsed;
                        loadingControl.IsVisible = false;
                        NavigationService.Navigate(new Uri("/View/MainPage.xaml", UriKind.Relative));
                    }
                    else
                    {
                        loadingControl.Visibility = Visibility.Collapsed;
                        loadingControl.IsVisible = false;
                        MessageBox.Show("Login failed! Please try again.", "Error", MessageBoxButton.OK);
                    }
                });
        }
    }
}